package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.CommodityLabelSaveBO;
import com.ztgf.commodity.models.dto.CommodityLabelDTO;
import com.ztgf.commodity.models.service.ICommodityLabelService;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cms/CommodityLabel")
@Api(value = "commodityLabelController", tags = "商品标签表cms接口")
public class CommodityLabelController {

    @DubboReference
    private ICommodityLabelService commodityLabelService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加标签", notes = "添加标签")
    public Result add(CommodityLabelSaveBO saveBO) {
        boolean flag = commodityLabelService.addLabel(saveBO);
        return new Result<>(flag);
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改标签", notes = "修改标签")
    public Result update(CommodityLabelSaveBO bo) {
        if (null == bo.getId()) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = commodityLabelService.updateLabel(bo);
        return new Result<>(flag);
    }

    @PostMapping(value = "/queryPageList")
    @ApiOperation(value = "查询标签列表", notes = "查询标签列表")
    public Result queryPageList(Integer pageNo, Integer pageSize) {
        Page<CommodityLabelDTO> page = new Page<>(pageNo, pageSize);
        Wrapper<CommodityLabelDTO> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        Page<CommodityLabelDTO> selectPage = commodityLabelService.selectPage(page, wrapper);
        return new Result<>(selectPage);
    }

    @PostMapping(value = "/queryList")
    @ApiOperation(value = "查询标签集合", notes = "查询标签集合")
    public Result queryList() {
        Wrapper<CommodityLabelDTO> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        List<CommodityLabelDTO> list = commodityLabelService.selectList(wrapper);
        return new Result<>(list);
    }

    @PostMapping(value = "/getById")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    public Result getById(Long id) {
        if (null == id) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        CommodityLabelDTO dto = commodityLabelService.selectById(id);
        return new Result<>(dto);
    }


}
